Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
Making a "CLI-like" progress bar in HTML/JS

So I'm working on a command line interface in the web. Currently, there is the start of a progress bar on the left of the CLI, and a few commands that work. I want to add more commands, but I felt that they needed to have a time delay feature to make it feel more authentic.

I want to make a function that, when called, will divide the (ms) used in the scenario by 12 (the number of dashes in the progress bar) and display an update every (ms).

The Progress bar looks something like this: [------------]

https://codepen.io/ZacV/pen/abEYpLz

function statsBar(ms){
  var timeChunk = Math.round(ms/12)
  for (let i = 0; i < timeChunk; i++) {
    document.getElementById("DispStatus").innerHTML = "[" + ("|" * timeChunk) + ("-" * (12 - timeChunk)) + "]"
    sleep(timeChunk);
  }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Ok. After a bit of trial and error, I found the most efficient way to finish Rocky Sims snippet. Rocky Sims' code created a really nice and efficient progress bar, however the size of the bar was inconsistent. This final product includes a progress bar with a consistent width to match with the width of the div. Thanks for all of your guys' help on this!

function statsBar(ms){
  var timeChunk = Math.round(ms/12);
  for (let i = 0; i <= 12; i++) {
    setTimeout(() => {
      document.getElementById("DispStatus").innerHTML = "[" + ('|'.repeat(i)) + ('-'.repeat(12 - i)) + "]";
    }, i * timeChunk);
  }
}
statsBar(6000);

<div id="DispStatus"></div>
about 4 years ago · Juan Pablo Isaza Report

0

You can use setTimeout to schedule updates to the progress bar all at once but have them happen over time.

function statsBar(ms){
  var timeChunk = Math.round(ms/12);
  for (let i = 0; i < 12; i++) {
    setTimeout(() => {
      document.getElementById("DispStatus").innerHTML = "[" + ('-'.repeat(i)) + "]";
    }, i * timeChunk);
  }
}
statsBar(6000);
<div id="DispStatus"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!